/*
 * @Description: 
 * @Author: lgt
 * @Date: 2021-06-26 16:05:27
 * @LastEditTime: 2021-06-27 18:20:40
 * @LastEditors: xxx
 */
const requireDirectory = require('require-directory');
const Router = require('koa-router');

class InitManager {
    static initCore(app){
        //入口方法
        InitManager.app = app;
        InitManager.initLoadRouters()
        InitManager.loadHttpException()
        InitManager.loadConfig()
    }
    static initLoadRouters(app) {
        const apiDirectory = `${process.cwd()}/app/api`; //获取绝对路径
        const modules = requireDirectory(module, apiDirectory, {
            visit: whenLoadModule
        })

        function whenLoadModule(obj) {
            if (obj instanceof Router) {
                InitManager.app.use(obj.routes())
            }
        }
    }
    static loadConfig(path = '') {
        const configPath = path || process.cwd() + '/config/config.js'
        const config = require(configPath)
        global.config = config
    }
    static loadHttpException() {
        const errors = require('./http-exception')
        global.errs = errors
    }
}

module.exports = InitManager;